﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using Modles.DTO;
using Modles.Models;
using Collection = Modles.Models.Collection;

namespace BilbilDAL
{
    public class CollectionDAL
    {
        /// <summary>
        /// 根据前端发送的用户信息表id查询并返回收藏表，按发布时间降序排序
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<VideoInfo_DTO> GetCollectionList(int userInfoId,int page, int limit, out int totalCount)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                IQueryable<VideoInfo_DTO> iquery = from a in con.Collections
                                                   where a.UserInfoId == userInfoId
                                                   join b in con.VideoInfos
                                                   on a.VideoInfoId equals b.Id
                                                   join c in con.Videos on b.VideoId equals c.Id
                                                   join d in con.Images on b.ImageId equals d.Id
                                                   join e in con.VideoTypes on b.VideoTypeId equals e.Id
                                                   join f in con.UserInfos on b.UserInfoId equals f.Id
                                                   join coll in con.Collections on b.Id equals coll.VideoInfoId into collections
                                                   select new VideoInfo_DTO
                                                   {
                                                       Id = b.Id,
                                                       VideoId = b.VideoId,
                                                       ImageId = b.ImageId,
                                                       VideoTypeId = b.VideoTypeId,
                                                       UserInfoId = b.UserInfoId,
                                                       Title = b.Title,
                                                       Briefintroduction = b.Briefintroduction,
                                                       Label = b.Label,
                                                       Time = b.Time,
                                                       Like = b.Like,
                                                       Views = b.Views,
                                                       VideoAddress = c.Address,
                                                       ImageAddress = d.Address,
                                                       VideoType = e.Type,
                                                       UserName = f.Name,
                                                       UserAvatar = f.Avatar,
                                                       VideoCollection = collections.Count()
                                                   };
                totalCount = iquery.Count();
                return iquery.OrderByDescending(x => x.Time)
                    .Skip((page - 1) * limit)
                    .Take(limit)
                    .ToList();  
                    
            }
        }
        /// <summary>
        /// 根据用户信息id获取用户关注的视频数量
        /// </summary>
        /// <returns></returns>
        public int GetUserCollectionCount(int userInfoId)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                return con.Collections.Where(x => x.UserInfoId == userInfoId).Count();
            }
        }
        /// <summary>
        /// 根据用户信息id和视频信息id添加关注记录
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        public bool AddCollectionInfo(int userInfoId, int videoInfoId)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                if (con.Collections.Where(x => x.UserInfoId == userInfoId && x.VideoInfoId == videoInfoId).Any()) { return false; }
                con.Collections.Add(new Collection() { UserInfoId = userInfoId, VideoInfoId = videoInfoId});
                return con.SaveChanges() > 0;
            }
        }
        /// <summary>
        /// 根据用户信息id和视频信息id删除关注记录
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        public bool DeleteCollectionInfo(int userInfoId, int videoInfoId)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                Collection collection = con.Collections.Where(x => x.UserInfoId == userInfoId && x.VideoInfoId == videoInfoId).FirstOrDefault();
                if(collection == null) { return false; }
                con.Remove(collection);
                return con.SaveChanges() > 0;
            }
        }
        /// <summary>
        /// 获取用户是否收藏以及视频收藏量
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="videoInfoId"></param>
        /// <param name="CollectionCount"></param>
        /// <returns></returns>
        public bool GetCollection(int userInfoId, int videoInfoId, out int CollectionCount)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                CollectionCount = con.Collections.Where(x => x.VideoInfoId == videoInfoId).Count();
                return con.Collections.Where(x => x.UserInfoId == userInfoId && x.VideoInfoId == videoInfoId).Any();
            }
        }
    }
}
